package com.haipumi.back.actions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.haipumi.beans.DataGrid;
import com.haipumi.beans.haipumi_skin;
import com.haipumi.biz.IBackSkinBiz;
import com.haipumi.utils.CommonData;
import com.haipumi.utils.RequestUtil;

public class BackSkinAction extends CommonAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8647271778316072911L;

	@Autowired
	IBackSkinBiz backSkinBiz;
	
	private haipumi_skin newskin;


	private DataGrid dg=new DataGrid();
	
	public void setDg(DataGrid dg) {
		this.dg = dg;
	}

	public DataGrid getDg() {
		return dg;
	}


	public String doSkin(){
		String op=null;
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		op=request.getParameter("op");
		if("findallskin".equals(op)){
			findAllSkin(request,response);
		}else if("addskin".equals(op)){
			addskin(request,response);
		}else if("updateskin".equals(op)){
			updateskin(request,response);
		}
		return super.SUCCESS;
	}



	private void updateskin(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			haipumi_skin skin=RequestUtil.parseRequest(request, haipumi_skin.class);
			newskin=backSkinBiz.updateSkin(skin);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		dg.setObj(newskin);
		updateApplication( );
	}



	private void addskin(HttpServletRequest request,
			HttpServletResponse response) {
		boolean isNewRecord=Boolean.parseBoolean(request.getParameter("isNewRecord"));
		haipumi_skin skin=null;
		if(isNewRecord){
			//是添加操作
			try {
				skin=RequestUtil.parseRequest(request, haipumi_skin.class);
				newskin=backSkinBiz.insertSkin(skin);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
		dg.setObj(newskin);
		updateApplication( );
	}



	private void findAllSkin(HttpServletRequest request,
			HttpServletResponse response) {
		List<haipumi_skin> list=null;
		list=backSkinBiz.findAllSkin(start,end,order,sort);
		//dg=new DataGrid();
		dg.setRows(list);
		int total=backSkinBiz.countAllSkin(start,end,order,sort);
		dg.setTotal(total);
		updateApplication( );
	}

	//更新Application中的值 
	private void updateApplication( ){
		//先查询所有的皮肤
		List<haipumi_skin> list=backSkinBiz.findAllDisplaySkin( );
		List<haipumi_skin> jdList=new ArrayList<haipumi_skin>();//经典背景图片
		List<haipumi_skin> jjList=new ArrayList<haipumi_skin>();//季节背景图片
		List<haipumi_skin> rwList=new ArrayList<haipumi_skin>();//人物背景图片
		List<haipumi_skin> xqxList=new ArrayList<haipumi_skin>();//小清新背景图片
		Map skinMap=new HashMap<String,List<haipumi_skin>>();//所有类别的背景图片集合
		for(int i=0;i<list.size();i++){//将所有的背景图片分类
			String imageName=list.get(i).getSimage();
			String type=imageName.substring(0, imageName.indexOf("_"));
			if("jd".equals(type)){
				jdList.add(list.get(i));
			}else if("jj".equals(type)){
				jjList.add(list.get(i));
			}else if("rw".equals(type)){
				rwList.add(list.get(i));
			}else if("xqx".equals(type)){
				xqxList.add(list.get(i));
			}
		}
		skinMap.put("jdList", jdList);
		skinMap.put("jjList", jjList);
		skinMap.put("rwList", rwList);
		skinMap.put("xqxList", xqxList);
		
		ServletContext application=request.getSession().getServletContext(); 
		application.setAttribute(CommonData.APPLICATIONSKINLIST, skinMap);
	}
}
